package com.wipz.eoa.iphone.user;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.toft.core2.UserInfo;
import com.toft.utils.JSONUtils;
import com.wipz.eoa.iphone.domain.MenuModel;

public class UserLoginService {
	public String getModuleTreeList(UserInfo userInfo) {

		String json = "[]";
		try {
			// 所有机构对应的菜单集合
			Map menusMap = userInfo.getMenus();
		
			// 当前机构对应的模组Map集合
			Map currentModulesMap = (Map) menusMap.get(userInfo.getUnitId());

			Set keySet = currentModulesMap.keySet();

			Iterator keyIt = keySet.iterator();

			List menus = new ArrayList(255);
			Map menuIndex = new HashMap(255);
			

			MenuModel menuModel = null;

			String id;
			String parentId;
			int level = 0;
			Map node = null;

			while (keyIt.hasNext()) {

				String key = keyIt.next().toString();

				List menuList = (List) currentModulesMap.get(key);

				for (int i = 0, n = menuList.size(); i < n; ++i) {
					menuModel = (MenuModel) menuList.get(i);

					id = menuModel.getId();
					parentId = menuModel.getParentId();

					if ("".equals(parentId)) {
						level = 0;
					} else if (menuIndex.containsKey(parentId)) {

						node = (Map) menus.get(new Integer(String.valueOf( menuIndex.get(parentId))).intValue());

						level = Integer.parseInt(node.get("NODELEVEL").toString()) + 1;
						node.put("HASCHILD", "1");
					}

					node = new HashMap(5);
					node.put("NODELEVEL", "" + level);
					node.put("NODENAME", menuModel.getName());
					node.put("LEVELCODE", id);
					node.put("URL", getMenuUrl(menuModel));
					node.put("HASCHILD", "0");
					node.put("PARENTID", menuModel.getParentId() == null? "": menuModel.getParentId());
					node.put("PICTUREID",menuModel.getPictureName());
					menus.add(node);
					menuIndex.put(id, new Integer(menus.size() - 1));
				}

			}
			
			List roots = new ArrayList(255);
			Map nodes = new HashMap(menus.size());
			
			for (int i = 0, n = menus.size(); i < n; ++i) {
				node = (Map) menus.get(i);
				parentId = node.get("PARENTID").toString();
				id = node.get("LEVELCODE").toString();
				
				if ("".equals(parentId)) {
					roots.add(node);
				} else {
					if(null!=node &&  null!=nodes &&  nodes.containsKey(parentId)){
						((List) nodes.get(parentId)).add(node);
					}
					//((List) nodes.get(parentId)).add(node);
				}
				nodes.put(id, new ArrayList(255)); 
			}
			
			List sortedMenu = new ArrayList(menus.size());
			travel(sortedMenu, roots, nodes);

			
			
			json = JSONUtils.toJSON(sortedMenu);
			} catch (Exception e ) {
			    // ignored;
			} 
			return json;
		}

		private String getMenuUrl(MenuModel menu) {

			String url = menu.getUrl();

			String module = menu.getModuleId();

			if (StringUtils.isEmpty(module)) {
				module = "";
			} else {
				module = "&module=" + module;
			}
			if (!"".equals(url)) {
				if (url.indexOf("?") < 0) {
					url = url + "?funcid=" + menu.getId() + module + "&isOp="
							+ menu.getIsOp();
				} else {
					url = url + "&funcid=" + menu.getId() + module + "&isOp="
							+ menu.getIsOp();
				}
			}

			return url;
		}
		
		private void travel(List sortedMenu, List roots, Map nodes) {
			
			Map node = null;
			for (int i = 0, n = roots.size(); i < n; ++i) {
				node = (Map) roots.get(i);
				String parentid=node.get("PARENTID").toString();
				if(parentid.length()>0){
					sortedMenu.add(node);
				}
				travel(sortedMenu, (List) nodes.get(node.get("LEVELCODE").toString()), nodes);
			}
		}

}
